package mysql

/*
func Benchmark_InsertPgParking(b *testing.B) {
	configure.LoadConfig("../configure/configure.json")
	InitMysql()
	for i := 0; i < b.N; i++ {
		var d userdata.PgParkingData
		d.CellCount = 1000
		d.Address = fmt.Sprintf("addr_%d", i)
		d.Name = fmt.Sprintf("park_name_%d", i)
		d.Owner = fmt.Sprintf("Owner_%d", i)
		d.OwnerPhone = fmt.Sprintf("%11d", i)
		d.Type = i % 3
		_, err := InsertPgParking(&d)
		if err != nil {
			fmt.Println("InsertPgParking:", err)
		}
	}
}
*/
func genBenchmark(prefix string, head string, body string, packageName string) string {
	tabname := parseTableName(head)
	typeName := underlineToUpper(tabname) + suffix
	keys := parseKey(body)
	if tabname == "" || keys == nil {
		return ""
	}
	// prefix := "Insert"
	con := "func Benchmark" + prefix + underlineToUpper(tabname) + "(b *testing.B) {\r\n" +
		`configure.LoadConfig("../configure/configure.json")
	InitMysql()
	for i := 0; i < b.N; i++ {
	` + "\r\n" +
		"var d " + packageName + "." + typeName + "\r\n"

	values := ""

	for _, s := range keys {
		ky := s[0]
		if ky == "id" || ky == "create_time" || ky == "time_version" {
			continue
		}

		tp, _ := sqlTypeToGo(s[1])
		switch tp {
		case "int64", "double", "float", "int":
			if s[1] == "tinyint" {
				values = values + "d." + underlineToUpper(ky) + "= " + tp + "(i%5)\r\n"
			} else {
				values = values + "d." + underlineToUpper(ky) + "= " + tp + "(i)\r\n"
			}
		case "string":
			values = values + "d." + underlineToUpper(ky) + "= fmt.Sprintf(\"" + ky + "_%d\", i)\r\n"
		}
	}
	con = con + values + "_, err := " + prefix + underlineToUpper(tabname) + "(&d)\r\n" +
		"if err != nil {\r\n" +
		"fmt.Println(\"" + prefix + underlineToUpper(tabname) + ":\", err)\r\n}\r\n}\r\n}\r\n"

	return con
}

func genBenchmarkContent(head string, body string, packageName string) string {
	con := genBenchmark("Insert", head, body, packageName)

	return con
}
